package com.scitc.flutter_pleasure_video

import android.app.Activity
import android.content.Context
import android.widget.Toast
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
import io.flutter.Log

class ToastUtilPlugin: FlutterPlugin {

    private var channel : MethodChannel? = null

    companion object {
        var toastChannel: String="android_flutter/android_toast";
        @JvmStatic
        fun registerWith(registrar: PluginRegistry.Registrar) {
            val plugin = ToastUtilPlugin()
            plugin.setupChannel(registrar.messenger(), registrar.context())
        }
    }

    override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        setupChannel(binding.binaryMessenger, binding.applicationContext)
    }

    override fun onDetachedFromEngine(p0: FlutterPlugin.FlutterPluginBinding) {
        teardownChannel();
    }

    fun setupChannel(messenger: BinaryMessenger, context: Context) {
        channel = MethodChannel(messenger, toastChannel)
        val handler = ToastMethodCallHandlerImpl(context)
        channel?.setMethodCallHandler(handler)
    }

    private fun teardownChannel() {
        channel?.setMethodCallHandler(null)
        channel = null
    }

}

class ToastMethodCallHandlerImpl(var context: Context) : MethodChannel.MethodCallHandler {

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when(call.method){
            "showToast" -> {
                if (call.arguments != null) {
                    Toast.makeText(context, call.arguments.toString()+"1", Toast.LENGTH_SHORT).show()
                    print("android 原生的toast")
                    Log.d("aaaaa","android 原生的toast")
                } else {
                    Toast.makeText(context, "空值", Toast.LENGTH_SHORT).show()
                    print("android 原生的toast")
                    Log.d("aaaaa","android 原生的toast")
                }
            }
        }
    }
}